﻿// Created by Windward Studios - no copyright is claimed. This code can be used in
// any manner by anyone for any reason. There is no copyright of any kind on it. You may
// use it in commercial products. You may change it without sharing those changes.
// We ask that you keep the "created by Windward Studios" in a comment at the top.

using System.Data.Common;

using System.Collections;
using System.Xml;
using System;
using Kailua.WSSLists;
using System.Net;
using System.Web.Services.Protocols;
using log4net;
//using System.Collections.Generic;

namespace net.windward.utils.ado.SPList
{
	/// <summary>
	/// Represents an SharePoint List server.
	/// </summary>
	public class WrSPListServer : IWrServer
	{
        private readonly DbProviderFactory provider;       
        private readonly string name;
        /// <summary>
        /// Name of the provider used (in this case OleDb; called odbcProvider for consistancy across classes)
        /// </summary>
        protected readonly string odbcProvider;

        private static ILog log = LogManager.GetLogger(typeof(WrSPListServer));

		/// <summary>
		/// Create an SharePoint List server object.
		/// </summary>
		/// <param name="provider">The provider for this server.</param>
		/// <param name="odbcProvider">The provider for this connection.</param>
		/// <param name="name">The name of the server.</param>
        public WrSPListServer(DbProviderFactory provider, string odbcProvider, string name)
		{
            this.provider = provider;
            this.odbcProvider = odbcProvider;
            this.name = name;
		}

        #region IWrServer Members

        /// <summary>
        /// The name of the server.
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// All databases on this server. If a provider does not have the concept of databases (Oracle)
        /// it will always return 1 database.
        /// </summary>
        /// <param name="credentials">The user credentials to access the database.</param>
        /// <returns>All databases on this server.</returns>
        public IWrDatabase[] GetDatabases(WrCredentials credentials)
        {
            try
            {
                // Use arrayList and add all the databases, return them.
                ArrayList rtn = new ArrayList();

                Lists ls = new Lists();
                ls.PreAuthenticate = true;
                ls.Credentials = new NetworkCredential(credentials.Username, credentials.Password);
                ls.Url = name + @"/_vti_bin/lists.asmx";

                foreach (XmlNode list in ls.GetListCollection().ChildNodes)
                {
                    rtn.Add(new WrSPListDatabase(provider, "", Name, list.Attributes["Title"].Value + "    ," + list.Attributes["ID"].Value));
                }
                return (WrSPListDatabase[])rtn.ToArray(typeof(WrSPListDatabase));
            }
            catch (SoapException ex)
            {
                string detail = ex.Detail.OuterXml;
                throw new Exception(detail, ex);
            }
            catch (Exception ex)
            {
                if (log.IsWarnEnabled)
                    log.Warn("Error occured retrieving SharePoint list (OleDb) databases.", ex);
                throw;
            }
        }

        /// <summary>
        /// All databases on this server. If a provider does not have the concept of databases (Oracle)
        /// it will always return 1 database.
        /// </summary>
        /// <param name="connStr">The connection string to access the database.</param>
        /// <returns>All databases on this server.</returns>
        public IWrDatabase[] GetDatabases(string connStr)
        {
            /*** NEED TO IMPLEMENT ***/
            //Problem here is the lack of credentials
            //  Specifically, the connection string doesn't contain the authentication information
            //  So if "Use Connection String" checkbox in AutoTag is selected, no databases will populate
            
            throw new NotImplementedException();
        }

        #endregion

		/// <summary>
		/// Create a database object.
		/// </summary>
		/// <param name="database">The name of the database.</param>
		public IWrDatabase CreateDatabase(string database)
		{
            return new WrSPListDatabase(provider, odbcProvider, Name, database);
		}

        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance. </param>
        /// <returns>a 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        public int CompareTo(object obj)
        {
            WrSPListServer db = obj as WrSPListServer;
            if (db == null)
                return 1;
            return name.CompareTo(db.name);
        }
	}
}
